﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Inventory.API
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IAsyncExceptionFilter
    {

        public async Task OnExceptionAsync(ExceptionContext context)
        {
            // 记录异常（可以使用日志记录工具）


            // 设置响应
            context.Result = new ObjectResult(new
            {
                Error = "An unexpected error occurred.",
                Details = context.Exception.Message
            })
            {
                StatusCode = StatusCodes.Status500InternalServerError
            };

            context.ExceptionHandled = true; // 标记异常已处理
            await Task.CompletedTask;
        }
    }
    }
